{__NOLAYOUT__}
<layout name="no_nav" />

<!-- TOPBAR -->
<header class="top-bar flex-row">
  <a class="row-item row-item-auto top-bar-back" href="javascript:window.history.back()">返回</a>
  <h3 class="row-item top-bar-name">收货地址编辑</h3>
  <span class="row-item row-item-auto top-bar-menu"></span>
</header>


<div class="pr-address-edit">

  <form method="post" action="saveDeliverAddressPost" id="form" class="form">

    <div class="form-item">
      <label class="form-item-label">收货人</label>
      <input type="text" name="name" placeholder="请输入收货人姓名" value="{$deliverAddress.name}" data-nullmsg="请填写收货人信息">
    </div>

    <div class="form-item">
      <label class="form-item-label">手机号码</label>
      <input type="tel" name="tel" placeholder="填写正确手机号码" value="{$deliverAddress.tel}" data-nullmsg="请填写手机号码" data-type="m" data-errormsg="填写的手机号码格式不正确">
    </div>

    <div class="form-item">
      <label class="form-item-label">选择收货地址</label>
      <div class="flex-row">
        <select class="row-item getaddress province" name="province" data-nullmsg="请选择所在省" data-geturl="__CONTROLLER__/getCityPost" data-target="city">{$deliverAddress.provinceOptions}</select>
        <select id="city" class="row-item getaddress city" name="city" data-nullmsg="请选择所在市" data-geturl="__CONTROLLER__/getAreaPost" data-target="area">{$deliverAddress.cityOptions}</select>
      </div>
      <select id="area" class="area" name="area" data-nullmsg="请选择所在地区">{$deliverAddress.areaOptions}</select>
      <textarea name="address" placeholder="路名或街道地址，门牌号" data-nullmsg="请填写具体地址">{$deliverAddress.address}</textarea>
    </div>
    
    <div class="form-item">
     <input type="hidden" name="id" value="{$deliverAddress.id}">
     <input type="hidden" name="sid" value="{$sid}">
     <button type="submit" class="btn btn-orange pr-btn w100">保存收货地址</button>
    </div>
  </form>
</div>
 
<script type="text/javascript">
  $(function(){

    $(".getaddress").on("change", getAddress);

    function getAddress() {

      var value = $(this).val(),
          sid = {$sid},
          url = $(this).attr("data-geturl"),
          target = $(this).attr("data-target");

      APP.showLoading();
 
      $.post( url, {code: value, sid: sid}, function(response) {
        $("#" + target).html(response);
        APP.hideLoading();
      });

    };

    $("#form").on("submit", function(e) {
      e.preventDefault();
      APP.checkForm(this, postAddress);
    });

    function postAddress() {
      var data = $(this).serializeArray();
      $.post("__CONTROLLER__/saveDeliverAddressPost", data, function(response) {
        if ( response.error === 0 ) {
          window.location = document.referrer;
          APP.alert( "保存成功，正在返回" );
        } else {
          APP.alert( response.msg );  
        };
      });
    };

  });
</script>